import GameConst from "./GameConst";
import Utils from "./utils"
import CellObject from "./cell"
import Hud from "./hud"
import LevelManager, {
    LevelData
} from "./levelmanager";
import {
    BlockClearTarget
} from "./leveltarget"
import {
    ClearMarkTarget
} from "./leveltarget"
import {
    PushDownTarget
} from "./leveltarget"
import {
    ScoreTarget
} from "./leveltarget"
import {
    ColorTarget
} from "./leveltarget"
import {
    BushClearTarget
} from "./leveltarget"
import {
    WaterClearTarget
} from "./leveltarget"
import {
    InfiniteTarget
} from "./leveltarget"
import MenuManager from "./menumanager";
import Tutorial from "./tutorial";
import BonusIndicator from "./bonusindicator";
import EndBonusIndicator from "./EndBonusIndicator";
import Tips from "./tips";
import TraceData from "./traceData";



export default class Match3Level extends Laya.Sprite {
    constructor() {
        super();
        this.isPaused = false;
        this.currentMove = 0;
        this.isGenerated = false;
        this.isLocked = false;
        this._isHardLocked = false;
        this.isEnded = false;
        this.score = 0;
        this.pushPositions = [];
        this.objects = [];
        this.customMatches = new Array;
        this.comboAmount = 0;
        this.maxCombo = 0;
        this.comboTimer = Match3Level.COMBO_TIME;
        this.turnedOff = false;
        this.isSelectBlocked = false;
        this.fieldCount = 5;
        this.traceObj = new TraceData();
        this.timeSinceLastAction = 0;
        this.timeSinceLastComboRemove = 0;
        this.movesLeft = 0;
        this.showWinMenuIn = -1;
        this.playLoseIn = 1.3;
        this.isStuck = false;
        this.cellData = null;
        this.needToUpdateGemCache = false;
        // this.cachedSprites = {};
        this.showTargetIn = -1;
        this.needToUpdateBack = true;
        this.waterRemoveMove = 0;
        this.backLayer = new Laya.Sprite;
        this.markLayer = new Laya.Sprite;
        this.underGemLayer = new Laya.Sprite;
        this.gemLayer = new Laya.Sprite;
        this.blockLayer = new Laya.Sprite;
        this.gemDestroyLayer = new Laya.Sprite;
        this.blockDestroyLayer = new Laya.Sprite;
        this.bonusLayer = new Laya.Sprite;
        this.bonusIndicatorLayer = new Laya.Sprite;
        this.hudLayer = new Laya.Sprite;
        this.isUpdatingAfterMatch = false;
        Match3Level.instance = this;
        // var n = ["blue bomb", "blue horizontal", "blue vertical", "blue", "green bomb", "green horizontal", "green vertical", "green", "orange bomb", "orange horizontal", "orange vertical", "orange", "purple bomb", "purple horizontal", "purple vertical", "purple"];
        // for (var r = 0; r < n.length; ++r) {
        //     var i = Utils.createSpriteFromSpritesheet(n[r]);
        //     var s = i.getBounds();
        //     //i.cache(s.x, s.y, s.width, s.height);
        //     this.cachedSprites[n[r]] = i;
        // }
        this.addChild(this.backLayer);
        this.addChild(this.markLayer);
        this.addChild(this.underGemLayer);
        this.addChild(this.gemLayer);
        this.addChild(this.blockLayer);
        this.addChild(this.gemDestroyLayer);
        this.addChild(this.blockDestroyLayer);
        this.addChild(this.bonusLayer);
        this.addChild(this.bonusIndicatorLayer);
        this.addChild(this.hudLayer);
        // this.setFieldProps(GameConst.TILE_SIZE, 26, 104, GameConst.LEVEL_W, GameConst.LEVEL_H);
        this.setFieldProps(GameConst.TILE_SIZE, GameConst.OFFSET_X, 104, GameConst.LEVEL_W, GameConst.LEVEL_H);
        this.cells = new Array;
        for (var r = 0; r < this.fieldWidth; r++) {
            var o = new Array;
            for (var u = 0; u < this.fieldHeight; u++) {
                var a = new CellObject(r, u);
                o.push(a)
            }
            this.cells.push(o)
        }
        var f = new Laya.Image("back.png");
        // f.x = 6;
        f.y = GameConst.SHIFT_GAME;
        this.back = f;
        this.backLayer.addChildAt(this.back, 0);
        GameConst.level = this;
        // var l = new createjs.Text("20fps", "30px Arial Black", "#000000");
        // l.x = 0;
        // l.alpha = .3;
        // l.y = GameConst.SCREEN_H + GameConst.SHIFT_H - 20;
        // addChild(l, this.hudLayer);
        // this.fpsText = l;
        this.hud = new Hud;
    }
    /**
     * 同时消除2个高级果冻,造成连击
     * @param {Array} matchs 
     */
    combinateBonuses(matchs) {
        var arr = [];
        var len = matchs.length;
        for (var i = 0; i < len; ++i) {
            if (matchs[i].object && matchs[i].object.isBonus)
                arr.push(matchs[i])
        }
        if (arr.length > 1) {
            arr = arr.sort(function (a, b) {
                return a.getBonusType() == b.getBonusType() ? 0 : a.getBonusType() > b.getBonusType() ? -1 : 1
            });
            var cell1 = arr[0];
            var cell2 = arr[1];
            var bonus1Type = cell1.getBonusType();
            var bonus2Type = cell2.getBonusType();
            var isline1 = bonus1Type == 1 || bonus1Type == 2;
            var isline2 = bonus2Type == 1 || bonus2Type == 2;
            var isTutorial = MenuManager.instance.isOnTutorial() ? Tutorial.instance.getBonusComboCell(cell1, cell2) : null;
            var cell = isTutorial ? isTutorial : Math.random() > .5 ? cell1 : cell2;
            //两个线
            if (isline1 && isline2) {
                cell1.object.setBonusType(5);
                cell2.object.setBonusType(5);
                //线+块
            } else if ((isline1 || isline2) && (bonus1Type == 3 || bonus2Type == 3)) {
                cell.object.setBonusType(6);
            } else if (bonus1Type == 3 && bonus2Type == 3) {
                cell.object.setBonusType(7);
            } else if ((isline1 || isline2) && (bonus1Type == 4 || bonus2Type == 4)) {
                cell.object.setBonusType(8);
            } else if ((bonus1Type == 4 || bonus2Type == 4) && (bonus1Type == 3 || bonus2Type == 3)) {
                cell.object.setBonusType(9);
            } else if ((bonus1Type == 4 || bonus2Type == 4) && (bonus1Type == 4 || bonus2Type == 4)) {
                cell.object.setBonusType(10);
            }
            new Tips("combo", (cell1.pos.x + cell2.pos.x) / 2, (cell1.pos.y + cell2.pos.y) / 2);
        }
    }

    onWin () {
        //dp_share();
        if (this.isEnded)
            return;
        // this.hud.logo.setVisible(false);
        this.isEnded = true;
        this.destroyObjects();
        var e = this.getBounds();
        // this.cache(0, 0, GameConst.SCREEN_W, GameConst.FULL_SCREEN_H);
        var t = this.objects.length;
        for (var n = 0; n < t; ++n) {
            var r = this.objects[n];
            if (r && r.isDestroyed && r instanceof JumpText) {
                var i = r;
                i.finishMovement();
                i.destroy();
            }
        }
        var star = this.getStarAmount();
        this.hud.forceFullUpdate();
        LevelManager.instance.onLevelComplete(star, this.score);
        this.traceObj.stars = star;
        // var o = GameConst.levelData.levelNumber == GameConst.LEVEL_AMOUNT - 1 ? MenuManager.instance.result : MenuManager.instance.winMenu;
        var o = MenuManager.instance.winMenu;
        MenuManager.instance.show(o, false);
        this.trace(3);
    };
    onLose () {
        //dp_share();
        if (this.isEnded)
            return;
        // this.hud.logo.setVisible(false);
        this.isEnded = true;
        this.destroyObjects();
        // var e = this.getBounds();
        // this.cache(0, 0, GameConst.SCREEN_W, GameConst.FULL_SCREEN_H);
        MenuManager.instance.show(MenuManager.instance.loseMenu, false);
        this.trace(2);
    };

    onRestart(){
        this.trace(1);
    }
    trace(win){
        this.traceObj.win = win;   
        this.traceObj.score = this.score;     
        Utils.trace(this.traceObj);
    }
}

Match3Level.prototype.pause = function () {
    if (this.isPaused || this.isEnded)
        return;
    this.isPaused = true;
    MenuManager.instance.show(MenuManager.instance.pause, false);
};
Match3Level.prototype.unpause = function () {
    this.isPaused = false;
    // this.uncache();
    if (MenuManager.instance.current == MenuManager.instance.pause)
        MenuManager.instance.closeCurrent();
};
Object.defineProperty(Match3Level.prototype, "isActive", {
    get: function () {
        return this.parent != null;
    },
    enumerable: true,
    configurable: true
});
Match3Level.prototype.init = function (lvl) {
    this.reset();
    this.showTargetIn = .3;
    this.timeSinceLastComboRemove = 10;
    
    CellObject.waterCells = [];
    this.waterRemoveMove = 0;
    this.isUpdatingAfterMatch = false;
    this.showWinMenuIn = -1;
    // this.uncache();
    //SoundsManager.instance.reset();
    Laya.stage.addChild(this);
    this.isEnded = false;
    var n = 0;
    var r = 0;
    n = 0;
    r = 0;
    this.movesLeft = 99;
    var scores = [500, 1500, 3e3];
    var testData = null;
    var href = Utils.getQueryString('cfg',false);
    if (href) {
        testData = href;
    }
    var lvlData = testData ? new LevelData(lvl, testData) : LevelManager.instance.data[lvl];
    GameConst.levelData = lvlData;
    this.cellData = lvlData.cellData;
    var target = lvlData.targetData;
    n = lvlData.customData;
    r = lvlData.customData2;
    this.movesLeft = lvlData.movesLeft;
    scores = lvlData.starValues;

    this.traceObj.reset(lvl,scores[2]);
    switch (target) {
        case 0:
            this.target = new BlockClearTarget;
            break;
        case 1:
            this.target = new ClearMarkTarget;
            break;
        case 2:
            this.target = new PushDownTarget(n);
            break;
        case 3:
            this.target = new ColorTarget(n, r);
            break;
        case 4:
            this.target = new ScoreTarget(n);
            break;
        case 5:
            this.target = new WaterClearTarget;
            break;
        case 6:
            this.target = new BushClearTarget();
            break;
        case -1:
            this.target = new InfiniteTarget;
            break
    }
    this.target.scores = scores;
    this.score = 0;
    this.generate();
    this.needToUpdateGemCache = true;
    this.hud.show();
    this.needToUpdateBack = true;
    this.update(0);

};
Match3Level.prototype.starMoveAwardMode = function () {
    if (this.isEnded || this.isHardLocked)
        return;
    this.isEnded = true;
    var e = [];
    for (var n = 0; n < this.fieldWidth; ++n) {
        for (var r = 0; r < this.fieldHeight; ++r) {
            var cell = this.cells[n][r];
            if (cell && cell.object && !cell.object.isMoving && !cell.object.isBonus && !cell.object.isPushable) {
                e.push(cell);
            }
        }
    }
    new Tips("amazing",  GameConst.SCREEN_W / 2, GameConst.SHIFT_H + 400);
    var s = this.hud.movesText.x;
    var o = this.hud.movesText.y - 20;
    var u = this.movesLeft > 0;
    var a = [];
    var f = [];
    var l = [];
    for (var n = 0; n < this.fieldWidth; ++n)
        for (var r = 0; r < this.fieldHeight; ++r) {
            var cell = this.cells[n][r];
            if (cell && cell.object && cell.object.isBonus) {
                a.push(cell);
                f.push(cell.getBonusType());
                l.push(!u ? 0 : .5);
            }
        }
    var c = e.length;
    var h = 0;
    var p = -1;
    var d = this.movesLeft;
    while (d > 0) {
        if (c <= 0) {
            this.objects.push(GameConst.pool.getText("100", null, 0, s, o));
            this.movesLeft--;
        } else {
            var cell = e.splice(Utils.getInt(c), 1)[0];
            var v = .6 + h * .065;
            var m = 1 + Utils.getInt(3);
            f.push(m);
            var g = new EndBonusIndicator(s, o, cell, v, m);
            this.objects.push(g);
            a.push(cell);
            p = Math.max(p, g.totalTime);
            l.push(g.totalTime);
            ++h;
        }
        --c;
        d--;
    }
    var y = [];
    p += .2;
    var b = -1;
    c = a.length;
    for (var h = 0; h < c; ++h) {
        var cell = a[h];
        var w = this.getBonusGroup(cell, f[h]);
        v = l[h];
        var E = w.length;
        for (var S = 0; S < E; ++S) {
            var x = w[S];
            var T = this.getDistance(cell, x, false);
            var N = (!u ? 1 : 1.5) + v + T * GameConst.GEM_KILL_DELAY + GameConst.BonusDestroyAnimationTIME;
            b = Math.max(N, b);
            if (x == cell || a.indexOf(x) == -1)
                x.prepareToClear(N, f[h], -1, x == cell);
            y.push(x);
        }
        cell.scoreToAdd = this.getBonusScore(f[h], 0);
    }
    c = y.length;
    b += GameConst.GEM_KILL_DELAY;
    for (h = 0; h < c; ++h)
        y[h].setTempBlock(true, b);
    this.showWinMenuIn = 1.6;
};
Match3Level.prototype.getStarAmount = function () {
    var e = 0;
    for (var t = 1; t <= 3; ++t) {
        var n = this.target.scores[t - 1];
        if (this.score >= n)
            e = t;
    }
    return e;
};
Match3Level.prototype.destroyObjects = function () {
    var e = this.objects.length;
    for (var t = 0; t < e; ++t)
        this.objects[t].destroy();
    this.objects = [];
};

Match3Level.prototype.trySpawnBonus = function (matchs) {
    if (matchs == null)
        return;
    var n = matchs.length;
    if (n < GameConst.MIN_LINE_SIZE)
        return;
    var r = 0;
    var i = 0;
    var s = 0;
    var o = 0;
    var u = null;
    var a = [];
    for (r = 0; r < n; ++r)
        a.push(matchs[r]);
    matchs = a;
    while (true) {
        n = matchs.length;
        if (n < GameConst.MIN_LINE_SIZE)
            return;
        var f = [];
        for (r = 0; r < n; ++r)
            f[r] = false;
        var l = [];
        for (r = 0; r < n; ++r)
            l[r] = false;
        var c = [];
        var h = [];
        var p = 0;
        var d = [];
        for (r = 0; r < n; ++r) {
            var v = matchs[r].getType();
            if (v < 0)
                continue;
            s = matchs[r].x;
            o = matchs[r].y;
            i = p = 0;
            d = [];
            if (!f[r] && !(s - 1 >= 0 && this.cells[s - 1][o].getType() == v)) {
                while (true) {
                    u = s + i < this.fieldWidth ? this.cells[s + i][o] : null;
                    var m = matchs.indexOf(u);
                    if (u && u.getType() == v && m != -1) {
                        ++p;
                        d.push(u);
                        f[m] = true
                    } else {
                        if (p >= GameConst.MIN_LINE_SIZE) {
                            c.push(d);
                            h.push(p);
                        }
                        break
                    }
                    ++i;
                }
            }
            i = p = 0;
            d = [];
            if (!l[r] && !(o - 1 >= 0 && this.cells[s][o - 1].getType() == v)) {
                while (true) {
                    u = o + i < this.fieldHeight ? this.cells[s][o + i] : null;
                    var m = matchs.indexOf(u);
                    if (u && u.getType() == v && m != -1) {
                        ++p;
                        d.push(u);
                        l[m] = true
                    } else {
                        if (p >= GameConst.MIN_LINE_SIZE) {
                            c.push(d);
                            h.push(p)
                        }
                        break
                    }
                    ++i;
                }
            }
        }
        var g = c.length;
        for (r = 0; r < g; ++r) {
            d = c[r];
            var y = d.length;
            for (i = 0; i < y; ++i) {
                u = d[i];
                for (var b = 0; b < g; b++) {
                    if (r != b) {
                        var w = c[b];
                        m = w.indexOf(u);
                        if (m != -1) {
                            var E = w.length;
                            var S = m;
                            if (S < GameConst.MIN_LINE_SIZE)
                                S = 0;
                            var x = E - (m + 1);
                            if (x < GameConst.MIN_LINE_SIZE)
                                x = 0;
                            h[r] -= (E - (S + x)) * 1.1
                        }
                    }
                }
            }
        }
        var T = -1;
        var N = -1;
        for (r = 0; r < g; ++r) {
            var C = h[r];
            if (C > T || N == -1) {
                T = C;
                N = r
            }
        }
        if (T != -1 && N != -1) {
            d = c[N];
            y = d.length;
            for (r = 0; r < y; ++r) {
                m = matchs.indexOf(d[r]);
                matchs.splice(m, 1)
            }
            var k = this.getBonusByMatch(y);
            var L = y <= 4 ? 3 : y <= 5 ? 4 : 5;
            this.spawnBonus(k, d[0], d[L])
        }
        if (g <= 1 || matchs.length < GameConst.MIN_LINE_SIZE)
            break
    }
};
Match3Level.prototype.getBonusByMatch = function (e) {
    return e >= 6 ? 4 : e <= 4 ? Math.random() > .5 ? 1 : 2 : 3;
    return 10;
    var t = Math.random();
    return t < .15 ? 1 : t < .3 ? 2 : t < .7 ? 3 : 4
};

Match3Level.prototype.spawnBonus = function (bonusType, t, n, target) {
    if (typeof target === "undefined") {
        target = null;
    }

    var tutorial = MenuManager.instance.isOnTutorial() ? Tutorial.instance.getBonusSpawnCell() : null;
    for (var i = 0; i < 80; ++i) {
        var x = Utils.getInt(this.fieldWidth);
        var y = Utils.getInt(this.fieldHeight);
        var cell = tutorial ? tutorial : this.cells[x][y];
        var f = false;
        if (cell.object && !cell._isBlock && !cell.object.isBonus && !cell.object.isWaitingForDestruction && !cell.object.isMoving && !cell.isWaitingForClear && !cell.object.isPushable && !cell.object.isHighValue) {
            for (var l = 0; l < this.customMatches.length; ++l)
                if (this.customMatches[l].indexOf(cell) != -1) {
                    f = true;
                    break
                }
            if (!f) {
                if (target == null) {
                    this.objects.push(new BonusIndicator(t, n, cell, bonusType));
                } else
                    target.updateTarget(cell);
                return
            }
        }
    }
    if (target)
        target.destroy();
};
Match3Level.prototype.getBonusGroup = function (e, t, n) {
    if (typeof t === "undefined") {
        t = 0;
    }
    if (typeof n === "undefined") {
        n = null;
    }
    var r = t != 0 ? t : e.getBonusType();
    var i = [];
    switch (r) {
        case 1:
            for (var s = 0; s < this.fieldWidth; ++s) {
                var o = this.cells[s][e.y];
                if (o.isStable() || o.isBreakable)
                    i.push(o)
            }
            break;
        case 2:
            for (var u = 0; u < this.fieldHeight; ++u) {
                var a = this.cells[e.x][u];
                if (a.isStable() || a.isBreakable)
                    i.push(a)
            }
            break;
        case 3:
            for (s = -2; s <= 2; ++s) {
                for (u = -2; u <= 2; ++u) {
                    var f = e.x + s;
                    var l = e.y + u;
                    if (f >= 0 && l >= 0 && l < this.fieldHeight && f < this.fieldWidth) {
                        var c = this.cells[f][l];
                        if (c.isStable() || c.isBreakable)
                            i.push(c);
                    }
                }
            }
            break;
        case 4:
            var h = this.getMatchColor(e, n);
            var p = 200;
            var d = [];
            for (s = 0; s < this.fieldWidth; ++s)
                for (u = 0; u < this.fieldHeight; ++u) {
                    if (this.cells[s][u] && (this.cells[s][u].isStable() || this.cells[s][u].isBreakable) && this.cells[s][u].getType() == h && !this.cells[s][u].isWaterBlock && !(this.cells[s][u] != e && this.cells[s][u].object && this.cells[s][u].object.bonusType == 4))
                        d.push(this.cells[s][u])
                }
            var v = e && !n;
            if (v && d.indexOf(e) == -1)
                d.push(e);
            var m = d.indexOf(e);
            v = v && m >= 0;
            var g = 70;
            var y = d.length;
            while (g > 0 && p > 0 && y > 0) {
                var b = v ? m : Utils.getInt(y);
                v = false;
                --y;
                c = d[b];
                i.push(c);
                d.splice(b, 1);
                g--;
                p--;
            }
            break;
        case 5:
            for (s = 0; s < this.fieldWidth; ++s) {
                o = this.cells[s][e.y];
                if (o.isStable() || o.isBreakable)
                    i.push(o)
            }
            for (u = 0; u < this.fieldHeight; ++u) {
                a = this.cells[e.x][u];
                if (a.isStable() || o.isBreakable)
                    i.push(a)
            }
            break;
        case 6:
            for (s = 0; s < this.fieldWidth; ++s) {
                for (u = e.y - 1; u <= e.y + 1; ++u) {
                    if (s >= 0 && s < this.fieldWidth && u >= 0 && u < this.fieldHeight) {
                        o = this.cells[s][u];
                        if (o.isStable() || o.isBreakable)
                            i.push(o)
                    }
                }
            }
            for (s = e.x - 1; s <= e.x + 1; ++s) {
                for (u = 0; u < this.fieldHeight; ++u) {
                    if (s >= 0 && s < this.fieldWidth && u >= 0 && u < this.fieldHeight) {
                        o = this.cells[s][u];
                        if (o.isStable() || o.isBreakable)
                            i.push(o)
                    }
                }
            }
            break;
        case 7:
            for (s = -4; s <= 4; ++s) {
                for (u = -4; u <= 4; ++u) {
                    var f = e.x + s;
                    var l = e.y + u;
                    if (f >= 0 && l >= 0 && l < this.fieldHeight && f < this.fieldWidth) {
                        var c = this.cells[f][l];
                        if (c.isStable() || c.isBreakable)
                            i.push(c)
                    }
                }
            }
            break;
        case 8:
            var h = this.getMatchColor(e, n);
            p = 10;
            var d = [];
            for (s = 0; s < this.fieldWidth; ++s)
                for (u = 0; u < this.fieldHeight; ++u) {
                    c = this.cells[s][u];
                    if (c && c.object && this.cells[s][u].getType() == h && !c._isBlock && !c.object.isBonus && !c.object.isMoving)
                        d.push(c);
                }
            var g = 70;
            var y = d.length;
            while (g > 0 && p > 0 && y > 0) {
                var b = Utils.getInt(y);
                --y;
                c = d[b];
                i.push(c);
                if (c != e)
                    c.object.setBonusType(Math.random() > .5 ? 1 : 2);
                d.splice(b, 1);
                g--;
                p--;
            }
            break;
        case 9:
            var h = this.getMatchColor(e, n);
            p = 10;
            var d = [];
            for (s = 0; s < this.fieldWidth; ++s)
                for (u = 0; u < this.fieldHeight; ++u) {
                    c = this.cells[s][u];
                    if (c && c.object && this.cells[s][u].getType() == h && !c._isBlock && !c.object.isBonus && !c.object.isMoving)
                        d.push(c)
                }
            var g = 70;
            var y = d.length;
            while (g > 0 && p > 0 && y > 0) {
                var b = Utils.getInt(y);
                --y;
                c = d[b];
                i.push(c);
                if (c != e)
                    c.object.setBonusType(3);
                d.splice(b, 1);
                g--;
                p--;
            }
            break;
        case 10:
            for (s = 0; s < this.fieldWidth; ++s)
                for (u = 0; u < this.fieldHeight; ++u) {
                    c = this.cells[s][u];
                    if (c.isStable() || c.isBreakable)
                        i.push(c)
                }
            break
    }
    return i
};
Match3Level.prototype.trySpawnHighValue = function (e) {
    var t = e.length;
    var n = 0;
    var r = [];
    for (var i = 0; i < t; ++i) {
        r.push(e[i]);
        if (!e[i].object.isHighValue)
            ++n
    }
    var s = Math.floor(n / 6);
    for (var i = 0; i < s; ++i) {
        var o = false;
        while (t > 0) {
            var u = Utils.getInt(t);
            t--;
            var a = r.splice(u, 1)[0];
            for (var f = 0; f < 4; ++f) {
                var l = a.x + (f == 0 ? -1 : f == 1 ? 1 : 0);
                var c = a.y + (f == 2 ? -1 : f == 3 ? 1 : 0);
                if (l >= 0 && c >= 0 && l < this.fieldWidth && c < this.fieldHeight) {
                    var h = this.cells[l][c];
                    var p = !MenuManager.instance.isOnTutorial() || Tutorial.instance.checkHighValueCell(h);
                    if (p && h.object && !h.object.isMoving && !h.isWaitingForClear && !h.object.isBonus && !h.object.isHighValue && !h.object.isPushable && !h.isWaitingForDestruction && !h.isBlock() && !h.object.isWaitingForDestruction) {
                        o = true;
                        var d = a.minClearIn;
                        h.object.setHighValueIn(d > 0 ? d : .1);
                        break
                    }
                }
            }
            if (o)
                break
        }
    }
};
Match3Level.prototype.getBonusDelay = function (btype) {
    var t = 0;
    if (btype == 4)
        t += 20 / 30;
    else if (btype != 0 && btype < 8) {
        t += GameConst.BonusDestroyAnimationTIME;
    }
    return t
};
Match3Level.prototype.getDistanceDelay = function (e, t, n, r) {
    var i = n == 8 || n == 9 ? 1 : n == 4 ? 2 : this.getDistance(e, t, false);
    return GameConst.GEM_KILL_DELAY * i * Math.pow(.75, r)
};
Match3Level.prototype.getMatchColor = function (e, t) {
    if (!t) {
        if (e.getBonusType() == 4)
            return Utils.getInt(this.assetNumber);
        return e.getType()
    }
    var n = [0, 0, 0, 0, 0, 0];
    var r = n.length;
    for (var i = 0; i < r; ++i) {
        var s = t[i].getType();
        if (s >= 0) {
            n[s]++;
            if (n[s] >= 2)
                return s
        }
    }
    return e.getType()
};
Match3Level.prototype.onHudDown = function (e, t) {
    if (this.hud)
        this.hud.onDown(e, t)
};
Object.defineProperty(Match3Level.prototype, "hasComboRecently", {
    get: function () {
        return this.timeSinceLastComboRemove < .2
    },
    enumerable: true,
    configurable: true
});
Match3Level.prototype.onMouseDown = function (e, t) {
    if (!this.isActive || this.hasComboRecently)
        return;
    if (this.hud)
        this.hud.onDown(e, t);
    if (this.isHardLocked || this.movesLeft <= 0 || this.isPaused || this.isWaitingForTarget)
        return;
    var n = this.stageToGridX(e);
    var r = this.stageToGridY(t);
    var w = Math.floor(n);
    var h = Math.floor(r);
    var matchs = this.getFillZone(w, h);
    if (matchs && matchs.length >= 3) {
        this.isLocked = true;
        ++this.currentMove;
        this.timeSinceLastComboRemove = 0;
        --this.movesLeft;
        this.traceObj.moves++;
        LevelManager.instance.moves++;
        this.combinateBonuses(matchs);
        for (var e = 0; e < this.fieldWidth; ++e)
            for (var t = 0; t < this.fieldHeight; ++t)
                this.cells[e][t].isTeleportLocked = false;
        var checkData = [{
            match: matchs,
            'main_cell': this.cells[w][h],
            delay: .15,
            bonus: 0,
            gen: 0,
            prevBonus: 0
        }];
        var index = 0;
        var time = -1;
        var first = true;
        this.isUpdatingAfterMatch = true;
        while (index < checkData.length) {
            var cells = checkData[index].match;
            var main_cell = checkData[index].main_cell;
            var delay = checkData[index].delay;
            var bonus = checkData[index].bonus;
            var gen = checkData[index].gen;
            var m = checkData[index].prevBonus;
            ++index;
            this.customMatches.push(cells);
            var len = cells.length;
            for (var y = 0; y < len; ++y) {
                var cell = cells[y];
                var E = delay + this.getDistanceDelay(main_cell, cell, bonus, gen) + this.getBonusDelay(cell.getBonusType());
                if (cell && cell.object && cell.object.isBonus && !cell.object.isMoving && !cell.isWaitingForClear) {
                    var S = cell.object.bonusType == 1 || cell.object.bonusType == 2;
                    var x = bonus == cell.object.bonusType && S ? bonus == 1 ? 2 : 1 : cell.object.bonusType;
                    cell.object.bonusType = x;
                    var T = this.getBonusGroup(cell, 0, cells);
                    if (T.length > 0) {
                        checkData.push({
                            match: T,
                            'main_cell': cell,
                            delay: E,
                            bonus: x,
                            gen: gen + 1,
                            prevBonus: bonus
                        })
                    }
                }
                if (cell.object && cell.object.isHighValue)
                    cell.scoreToAdd = this.getHighValueCellScore() + (cell.scoreToAdd >= 0 ? cell.scoreToAdd : 0);
                time = Math.max(time, E);
                cell.prepareToClear(E, bonus)
            }
            if (first) {
                var N = this.getComboScore(len);
                var t = -1;
                var C = null;
                for (var y = 0; y < len; ++y) {
                    var cell = cells[y];
                    var k = cell.minClearIn;
                    if (cell.object && !cell.object.isBonus && cell.isWaitingForClear && k > t) {
                        t = k;
                        C = cell
                    }
                }
                if (C) {
                    C.scoreToAdd = N + (cell.scoreToAdd >= 0 ? cell.scoreToAdd : 0);
                    C.bonusComboCount = index
                }
            } else {
                if (main_cell.object && main_cell.object.isBonus) {
                    main_cell.scoreToAdd = this.getBonusScore(bonus, gen - 1, m) + (main_cell.scoreToAdd >= 0 ? main_cell.scoreToAdd : 0);
                    main_cell.bonusComboCount = index
                }
            }
            first = false
        }
        time += GameConst.GEM_KILL_DELAY * (this.customMatches.length <= 1 ? 3 : 7);
        if (GameConst.needTutorial && GameConst.episode == 3 && this.currentMove == 2 && GameConst.levelData.levelNumber == 1)
            time += 1;
        for (y = 0; y < this.customMatches.length; ++y)
            for (var L = 0; L < this.customMatches[y].length; ++L)
                this.customMatches[y][L].setTempBlock(true, time);
        this.customMatches = [];
        this.trySpawnBonus(matchs);
        this.trySpawnHighValue(matchs)
    } else {
        if (w < 0 || h < 0 || w >= this.fieldWidth || h >= this.fieldHeight)
            return;
        var cell = this.cells[w][h];
        if (cell && cell.object && this.isGoodForClick(cell))
            cell.object.playJellyAnimation(1, 1)
    }
};
Match3Level.prototype.getBonusScore = function (e, n, r) {
    if (typeof r === "undefined") {
        r = -1
    }
    if (r >= 8 && r <= 9)
        return -1;
    var i = Match3Level.bonusScores[e] * Math.pow(1.2, n);
    i = Math.round(i / 10);
    i *= 10;
    return i
};
Match3Level.prototype.getHighValueCellScore = function () {
    return 30
};
Match3Level.prototype.getComboScore = function (e) {
    return e * CellObject.BASE_SCORE
};
Match3Level.prototype.getDistance = function (e, t, n) {
    if (typeof n === "undefined") {
        n = false
    }
    var r = Math.abs(e.x - t.x);
    if (n)
        r = Math.min(r, Math.abs(r - this.fieldWidth));
    var i = Math.abs(e.y - t.y);
    if (n)
        i = Math.min(i, Math.abs(i - this.fieldHeight));
    return r + i
};
Match3Level.prototype.onBeforeSwap = function (e) {};
Match3Level.prototype.onExit = function () {};
Match3Level.prototype.onWrongMove = function () {};
Match3Level.prototype.onSuccessMove = function (e, t, n, r) {};
Match3Level.prototype.handleCombo = function () {};
Match3Level.prototype.canSwap = function (e, t) {
    if (e.isBlock() || t.isBlock())
        return {
            colorType: -1,
            color: -1
        };
    return {
        colorType: 0,
        color: 0
    };
    var n = e.x - 2;
    var r = e.x + 2;
    var i = e.y - 2;
    var s = e.y + 2;
    n = n < 0 ? 0 : n >= this.fieldWidth ? this.fieldWidth - 1 : n;
    i = i < 0 ? 0 : i >= this.fieldHeight ? this.fieldHeight - 1 : i;
    r = r < 0 ? 0 : r >= this.fieldWidth ? this.fieldWidth - 1 : r;
    s = s < 0 ? 0 : s >= this.fieldHeight ? this.fieldHeight - 1 : s;
    var o = -1;
    var u = 0;
    var a = e.y;
    for (var f = n; f <= r; f++) {
        var l = this.cells[f][a] == t ? e.getType() : this.cells[f][a] == e ? t.getType() : this.cells[f][a].getType();
        if (l == this.assetNumber) {
            var c = f + 1 <= r ? this.cells[f + 1][a] : null;
            var h = !c ? -2 : c == e ? t.getType() : c == t ? e.getType() : c.getType();
            if (u == 0 && c) {
                o = h;
                u = 1
            } else if (u == 2) {
                return {
                    colorType: 0,
                    color: o
                }
            } else if (u == 1 && c) {
                if (o == h || h == this.assetNumber) {
                    return {
                        colorType: 0,
                        color: o
                    }
                } else {
                    o = h;
                    u = 2;
                    f++
                }
            }
        } else if (l == -1) {
            o = -1;
            u = 0
        } else if (o == -1 || l != o) {
            o = l;
            u = 1
        } else if (o == l) {
            u++;
            if (u >= 3) {
                return {
                    colorType: 0,
                    color: o
                }
            }
        }
    }
    o = -1;
    u = 0;
    f = e.x;
    for (a = i; a <= s; a++) {
        l = this.cells[f][a] == t ? e.getType() : this.cells[f][a] == e ? t.getType() : this.cells[f][a].getType();
        if (l == this.assetNumber) {
            c = a + 1 <= s ? this.cells[f][a + 1] : null;
            h = !c ? -2 : c == e ? t.getType() : c == t ? e.getType() : c.getType();
            if (u == 0 && c) {
                o = h;
                u = 1
            } else if (u == 2) {
                return {
                    colorType: 1,
                    color: o
                }
            } else if (u == 1 && c) {
                if (o == h || h == this.assetNumber) {
                    return {
                        colorType: 1,
                        color: o
                    }
                } else {
                    o = h;
                    u = 2;
                    a++
                }
            }
        } else if (l == -1) {
            o = -1;
            u = 0
        } else if (o == -1 || l != o) {
            o = l;
            u = 1
        } else if (o == l) {
            u++;
            if (u >= 3) {
                return {
                    colorType: 1,
                    color: o
                }
            }
        }
    }
    if ((e.object == null || t.object == null) && !(e.object == null && t.object == null)) {
        var p = e.object == null ? e : t;
        var d = e.object == null ? t : e;
        o = -1;
        u = 0;
        f = d.x;
        for (a = this.fieldHeight - 1; a >= 0; a--) {
            if (a == d.y)
                continue;
            l = this.cells[f][a].getType();
            if (l == this.assetNumber) {
                var v = a - 1 == d.y ? a - 2 : a - 1;
                c = v >= 0 ? this.cells[f][v] : null;
                h = !c ? -2 : c.getType();
                if (u == 0 && c) {
                    o = h;
                    u = 1
                } else if (u == 2) {
                    return {
                        colorType: 2,
                        color: o
                    }
                } else if (u == 1 && c) {
                    if (o == h || h == this.assetNumber) {
                        return {
                            colorType: 2,
                            color: o
                        }
                    } else {
                        o = h;
                        u = 2;
                        a++
                    }
                }
            } else if (l == -1) {
                o = -1;
                u = 0
            } else if (o == -1 || l != o) {
                o = l;
                u = 1
            } else if (o == l) {
                u++;
                if (u >= 3) {
                    return {
                        colorType: 2,
                        color: o
                    }
                }
            }
        }
    }
    return {
        colorType: -1,
        color: -1
    }
};
Match3Level.prototype.getCustomMatchesAmount = function () {
    var e = 0;
    for (var t in this.customMatches)
        for (var n in this.customMatches[t])
            e++;
    return e
};
Object.defineProperty(Match3Level.prototype, "isHardLocked", {
    get: function () {
        return this.isLocked || this._isHardLocked
    },
    enumerable: true,
    configurable: true
});
Match3Level.prototype.update = function (e) {
    if (!this.isGenerated)
        return;
    if (!this.isPaused && this.isActive && !GameConst.game.isHidden)
        this.traceObj.usetime += e;
    if (this.comboTimer > 0) {
        this.comboTimer--;
        if (this.comboTimer == 0) {
            if (this.comboAmount > this.maxCombo)
                this.maxCombo = this.comboAmount;
            this.handleCombo();
            this.comboAmount = 0;
            this.setCombo()
        }
    }
    this.timeSinceLastComboRemove += e;
    var empty = 0;
    this._isHardLocked = false;
    this.isLocked = this.hasComboRecently || this.isUpdatingAfterMatch || this.isStuck;
    for (var n = 0; n < this.fieldWidth; ++n)
        for (var r = 0; r < this.fieldHeight; ++r) {
            var cell = this.cells[n][r];
            cell.update(e);
            if (cell.object == null && !cell.isBlock()){
                ++empty;
            }
            this.isLocked = this.isLocked || cell.object && (cell.object.isMoving || cell.object.isWaitingForDestruction) || cell.isWaitingForClear || cell.isChangingType;
            this._isHardLocked = this._isHardLocked || this.isLocked || cell.isTempBlock && !cell._isBlock;
        }
    var s = this.objects.length;
    for (var o = 0; o < s; ++o) {
        var u = this.objects[o];
        if (u && !u.isDestroyed) {
            u.update(e);
            this.isLocked = this.isLocked || u.isLocked && !u.isDestroyed
        } else {
            this.objects.splice(o, 1);
            o--;
            s--
        }
    }
    this.isUpdatingAfterMatch = false;
    if (empty > 0) {
        var a = this.addObjects();
        a += this.pushObjects();
        this.isLocked = this.isLocked || a > 0
    }
    if (this.isHardLocked || this.isEnded)
        this.timeSinceLastAction = 0;
    else {
        this.timeSinceLastAction += e;
        if (this.timeSinceLastAction > 3 && !MenuManager.instance.isOnTutorial())
            this.playHint();
        if (!this.isHardLocked)
            this.checkIfComboExists()
    }
    if (this.showWinMenuIn > 0 && !this.isHardLocked) {
        this.showWinMenuIn -= e;
        if (this.showWinMenuIn <= 0) {
            this.isEnded = false;
            this.onWin()
        }
    }
    this.isLocked = this.isLocked || this.isEnded;
    if (!this.isPaused && (!this.isHardLocked || this.isStuck) && !this.isEnded && !this.target.isCompleted() && (this.movesLeft <= 0 || this.isStuck) && this.playLoseIn > 0 && !MenuManager.instance.target.isMenuActive) {
        this.playLoseIn -= e;
        if (this.playLoseIn <= 0)
            this.onLose()
    }
    if (this.isWaitingForTarget && !this.isHardLocked && !this.isPaused) {
        this.showTargetIn -= e;
        if (!this.isWaitingForTarget) {
            // if (!Tutorial.instance.hasTutorial(GameConst.levelData.levelNumber) || !GameConst.needTutorial)
        MenuManager.instance.show(MenuManager.instance.target, false);
            // else
            //     MenuManager.instance.show(MenuManager.instance.tutorial, false)
        }
    }
    var f = CellObject.waterCells.length;
    if (f > 0 && this.waterRemoveMove < this.currentMove && !this.isHardLocked) {
        this.placeNewWater()
    }
    if (this.needToUpdateBack) {
        this.needToUpdateBack = false;
        // if (this.markLayer.cacheCanvas)
        //     this.markLayer.updateCache();
        // else
        //     this.markLayer.cache(0, 0, GameConst.SCREEN_W, GameConst.FULL_SCREEN_H)
    }
    this.hud.update(e);
    //this.fpsText.text = createjs.Ticker.getMeasuredFPS().toFixed(1)
};
Match3Level.prototype.placeNewWater = function () {
    var waterArr = CellObject.waterCells.concat();
    var waterLen = waterArr.length;
    var arr = [0, 1, 2, 3];
    while (waterLen > 0) {
        var wcellLen = Utils.getInt(waterLen);
        var wCell = waterArr.splice(wcellLen, 1)[0];
        waterLen--;
        if (!wCell.isWaterBlock)
            console.log("ERROR! CELL MUST BE WATER BLOCKED");
        var len = arr.length;
        while (len > 0) {
            var rndPosition = arr.splice(Utils.getInt(len), 1)[0];
            len--;
            var nX = wCell.x + (rndPosition == 0 ? -1 : rndPosition == 1 ? 1 : 0);
            var nY = wCell.y + (rndPosition == 2 ? -1 : rndPosition == 3 ? 1 : 0);
            if (nX >= 0 && nY >= 0 && nX < this.fieldWidth && nY < this.fieldHeight) {
                var cell = this.cells[nX][nY];
                if (cell && !cell.isWaitingForClear && cell.object && !cell.isBlock() && this.isGoodForClick(cell)) {
                    cell.setBlock(6);
                    this.waterRemoveMove = this.currentMove;
                    return
                }
            }
        }
        arr.push(0, 1, 2, 3)
    }
};
Match3Level.prototype.updateFpsText = function () {
    //this.fpsText.text = createjs.Ticker.getMeasuredFPS().toFixed(1)
};
Match3Level.prototype.checkIfComboExists = function () {
    if (this.hud.isShuffleActive() || this.isWaitingForTarget || MenuManager.instance.current == MenuManager.instance.target || this.isStuck)
        return;
    for (var e = 0; e < this.fieldHeight; ++e) {
        for (var t = 0; t < this.fieldWidth; ++t) {
            var n = this.cells[t][e];
            var r = this.getFillZone(t, e, 3);
            if (r && r.length >= 3) {
                return
            }
        }
    }
    var i = [];
    for (var t = 0; t < this.fieldWidth; ++t)
        for (var e = 0; e < this.fieldHeight; ++e)
            if (this.isGoodForClick(this.cells[t][e]))
                i.push(this.cells[t][e]);
    var s = i.length;
    this.isLocked = true;
    var o = 80;
    var u = false;
    var a = .6;
    while (o >= 0 && !u) {
        o--;
        var f = Utils.getInt(s);
        var n = i[f];
        if (!n.object.isPushable) {
            var l = [n];
            for (var c = 0; c < 4; ++c) {
                t = n.x + (c == 0 ? 1 : c == 1 ? -1 : 0);
                e = n.y + (c == 2 ? 1 : c == 3 ? -1 : 0);
                if (t >= 0 && e >= 0 && t < this.fieldWidth && e < this.fieldHeight) {
                    var h = this.cells[t][e];
                    if (this.isGoodForClick(h)) {
                        l.push(h);
                        if (l.length >= 3) {
                            var p = (n.getType() + 1) % this.assetNumber;
                            u = true;
                            for (var d = 0; d < l.length; ++d) {
                                l[d].prepareToChangeType(a +Utils.lerp(0, .2, Math.random()), p);
                                f = i.indexOf(l[d]);
                                if (f != -1) {
                                    i.splice(f, 1);
                                    s--
                                }
                            }
                            break
                        }
                    }
                }
            }
        }
    }
    if (!u) {
        this.isStuck = true;
        this.playLoseIn /= 1.5;
        return
    }
    while (s > 0) {
        var f = Utils.getInt(s);
        s--;
        var n = i.splice(f, 1)[0];
        n.prepareToChangeType(a +Utils.lerp(0, .2, Math.random()));
    }
    this.hud.playShuffleAnimation()
};
Match3Level.prototype.playHint = function () {
    var e = [];
    for (var t = 0; t < this.fieldWidth; ++t)
        for (var n = 0; n < this.fieldHeight; ++n)
            e.push(this.cells[t][n]);
    var r = 15;
    var i = e.length;
    while (--r > 0 && i > 0) {
        var s = Utils.getInt(i);
        i--;
        var o = e.splice(s, 1)[0];
        if (!this.isGoodForClick(o))
            continue;
        var u = this.getFillZone(o.x, o.y, 3);
        if (u && u.length >= 3) {
            this.timeSinceLastAction = -Math.random() * 4;
            u[Utils.getInt(u.length)].object.playJellyAnimation(.2 + Math.random() * .2, .25 + Math.random() * .25);
            return
        }
    }
};
Match3Level.prototype.setCombo = function () {};
Match3Level.prototype.isGoodForClick = function (cell) {
    return cell.object && cell.object.colorType != -1 && !cell.object.isMoving && !cell.isWaitingForClear && !cell.isObjectBlocked && cell.object.colorType != 4 && !cell.isChangingType
};
Match3Level.prototype.getFillZone = function (e, t, n) {
    if (typeof n === "undefined") {
        n = -1
    }
    if (e < 0 || t < 0 || e >= this.fieldWidth || t >= this.fieldHeight)
        return null;
    var r = GameConst.tempObjectData.fillZoneRes;
    r.length = 0;
    var i = GameConst.tempObjectData.fillZoneCheck;
    i.length = 0;
    var s = this.cells[e][t];
    if (!this.isGoodForClick(s))
        return r;
    var o = s.object.colorType;
    i.push(s);
    var u = 0;
    var a = i.length;
    while (u < a && (n < 0 || u < n)) {
        var f = i[u];
        r.push(f);
        ++u;
        for (var l = 0; l < 4; ++l) {
            var c = f.x + (l == 0 ? -1 : l == 1 ? 1 : 0);
            var h = f.y + (l == 2 ? -1 : l == 3 ? 1 : 0);
            if (c >= 0 && h >= 0 && c < this.fieldWidth && h < this.fieldHeight) {
                var p = this.cells[c][h];
                if (p.object && (p.object.colorType == o || p.object.bonusType == 4) && !p.object.isMoving && !p.isWaterBlock && i.indexOf(p) == -1) {
                    i.push(p);
                    ++a
                }
            }
        }
    }
    return r
};
Match3Level.prototype.onGroupRemove = function (e, t) {};
Match3Level.prototype.pushObjects = function () {
    var e = 0;
    e += this.pushBaseObjects();
    e += this.pushHoles();
    return e
};
Match3Level.prototype.pushBaseObjects = function () {
    var e = 0;
    for (var t = 0; t < this.fieldWidth; t++) {
        for (var n = this.fieldHeight - 1; n >= 0; n--) {
            var cell1 = this.cells[t][n];
            var isCellBlock = cell1._isBlock || cell1.isTempBlock;
            if (isCellBlock){
                continue;
            }
            if (cell1.object == null) {
                var s = -1;
                var o = false;
                var u = t;
                var isStartNew = false;
                for (var f = n; f >= 0; f--) {
                    var cell = this.cells[u][f];
                    var c = cell._isBlock || cell.isTempBlock;
                    if ((c || cell.isWaitingForClear) && f != n) {
                        o = true;
                        break
                    } else if (cell.object != null && !c && !cell.object.isMoving && f != n) {
                        s = f;
                        break
                    } else {
                        var teleStarCell = cell.teleportCellStart;
                        if(teleStarCell == null){
                            continue;
                        }
                        var p = teleStarCell.object != null && !teleStarCell._isBlock && !teleStarCell.isTempBlock && !teleStarCell.object.isMoving && !teleStarCell.isTeleportLocked;
                        if (p) {
                            //当前列是否有在移动
                            for (var d = f + 1; d < this.fieldHeight; ++d) {
                                var v = this.cells[u][d];
                                if (v && v.object && v.object.isMoving) {
                                    p = false;
                                    break
                                }
                            }
                            //没有移动的.可以
                            if (p) {
                                s = teleStarCell.y;
                                cell = teleStarCell;
                                isStartNew = true;
                                break
                            }
                        }
                    }
                }
                if (s != -1 && !o) {
                    if (!isStartNew) {
                        cell.object.moveTo(cell1.pos.x, cell1.pos.y, false, .05);
                        cell1.setObject(cell.object);
                        cell.object = null
                    } else
                        cell.prepareToTeleport(cell1);
                    ++e
                }
            }
        }
    }
    return e
};
Match3Level.prototype.pushHoles = function () {
    var e = 0;
    for (var t = 0; t < this.fieldWidth; ++t) {
        var n = this.isEnded;
        for (var r = 0; r < this.fieldHeight; ++r) {
            var i = this.cells[t][r];
            var s = i._isBlock || i.isTempBlock;
            var o = s || !i.isPushEnabled;
            var u = i.teleportCellStart != null && !i.teleportCellStart.isBlock() && i.teleportCellStart.object != null && !i.teleportCellStart.isBlock();
            if (u && !s) {
                n = false;
                continue
            }
            if (o) {
                n = true;
                if (s)
                    continue
            }
            if (i.object == null && n) {
                if (i.blockWasRemovedRecently)
                    continue;
                var a = Math.random() > .5;
                for (var f = 0; f < 2; f++) {
                    var l = a && f == 0 || !a && f == 1 ? Utils.cjp(t - 1, r - 1) : Utils.cjp(t + 1, r - 1);
                    if (!(l.x >= 0 && l.y >= 0 && l.x < this.fieldWidth && l.y < this.fieldHeight))
                        continue;
                    var c = this.cells[l.x][l.y];
                    if (c.isStable() && c.object != null && !c.isWaitingForClear && !c.isTempBlock && !c.isObjectBlocked) {
                        var h = i.pos;
                        c.object.moveTo(h.x, h.y, false, .05, true);
                        i.setObject(c.object);
                        c.object = null;
                        ++e;
                        break
                    }
                }
            } else if (i.object != null && n) {
                n = false
            }
        }
    }
    return e
};
Match3Level.prototype.addObjects = function (e, n) {
    if (typeof e === "undefined") {
        e = -1
    }
    if (typeof n === "undefined") {
        n = -1
    }
    if (this.isEnded)
        return 0;
    var r = 0;
    for (var col = 0; col < this.fieldWidth; col++) {
        if (e != -1 && col != e)
            continue;
        var s = false;
        for (var row = 0; row < this.fieldHeight; row++) {
            if (n != -1 && row != n)
                continue;
            if (this.cells[col][row].object || this.cells[col][row].isBlock() || !this.cells[col][row].isPushEnabled) {
                if (this.isGenerated)
                    break;
                else
                    s = true
            } else {
                var u = this.gridToStageX(col);
                var rowStart = -1;
                if (s) {
                    if (this.getCellDataColor(col, row) <= 0)
                        continue;
                    else
                        rowStart = row
                }
                var objType = this.getObjectType(col, row);
                var cellObj = GameConst.pool.getObject(this.cells[col][row], u, this.gridToStageY(rowStart), this.assetNumber, objType);
                if (rowStart != row)
                    cellObj.moveTo(u, this.gridToStageY(row));
                this.cells[col][row].setObject(cellObj);
                ++r
            }
        }
    }
    return r
};
Match3Level.prototype.getObjectType = function (col, row) {
    if (!this.isGenerated) {
        var n = this.getCellDataColor(col, row);
        return n - 1
    }
    return this.target.getExactType(col, row)
};
Match3Level.getCellDataType = function (data, col, row) {
    if (!data)
        return "";
    return data.charAt(2 * (row + col * GameConst.LEVEL_H) + 1)
};
Match3Level.prototype.getCellDataColor = function (e, t) {
    if (!this.cellData)
        return -1;
    return parseInt(this.cellData.charAt(2 * (t + e * this.fieldHeight) + 2))
};
Match3Level.prototype.generateCells = function () {
    for (var col = 0; col < this.fieldWidth; col++) {
        for (var row = 0; row < this.fieldHeight; row++) {
            var cell = this.cells[col][row];
            var type = Match3Level.getCellDataType(this.cellData, col, row);
            switch (type) {
                case "0":
                case "":
                    break;
                case "1":
                    cell.setBlock(0);
                    break;
                case "2":
                    cell.setBlock(1);
                    break;
                case "3":
                    cell.setBlock(2);
                    break;
                case "4":
                    cell.setBlock(4);
                    break;
                case "5":
                    cell.setMark(1);
                    break;
                case "6":
                    cell.setMark(2);
                    break;
                case "7":
                    cell.setBlock(5);
                    break;
                case "8":
                    cell.setBlock(6);
                    break;
                case "9":
                    cell.setMark(3);
                    break;
                case "a":
                    cell.setBlock(3);
                    break;
                case "b":
                    cell.setBlock(7);
                    break
            }
            var color = this.getCellDataColor(col, row);
            if (color == 5)
                cell.setBlock(4);
            else if (color == 6)
                cell.setBlock(7)
        }
    }
    if (GameConst.levelData.jellyPushPositions) {
        for (var col = 0; col < this.fieldWidth; ++col)
            this.cells[col][0].isPushEnabled = GameConst.levelData.jellyPushPositions[col]
    }
    var o = [];
    var u = 18;
    for (var col = 0; col < u; ++col)
        o.push([null, null]);
    var a = GameConst.levelData.teleportData.length;
    for (var col = 0; col < a; ++col) {
        var f = GameConst.levelData.teleportData[col];
        o[f[3]][f[2]] = this.cells[f[0]][f[1]]
    }
    for (col = 0; col < u; ++col) {
        if (o[col][0] && o[col][1]) {
            o[col][0].setTeleport(true, o[col][1]);
            o[col][1].setTeleport(false, o[col][0])
        }
    }
};
Match3Level.prototype.generateField = function () {
    this.addObjects();
    this.target.onLevelGenerated();
    // if (GameConst.levelData.levelNumber == 1) {
    //     this.cells[5][3].object.setBonusType(3);
    //     this.cells[2][4].object.setBonusType(1)
    // }
}
Match3Level.prototype.generate = function () {
    this.assetNumber = 4;
    this.generateCells();
    this.generateField();
    this.isGenerated = true;
    this.needToUpdateBack = true
};
Match3Level.prototype.getRandomType = function () {
    this.fieldCount++;
    var e = Utils.getInt(1e3);
    var t = e % this.assetNumber;
    return t
};
Match3Level.prototype.getChangeType = function () {
    return Utils.getInt(1e3) % this.assetNumber
};
Match3Level.prototype.setBackground = function (e) {
    this.background = e
};
Match3Level.prototype.setFieldProps = function (e, t, n, r, i) {
    this.tileSize = e;
    this.offsetX = t;
    this.offsetY = n + GameConst.SHIFT_H;
    this.fieldWidth = r;
    this.fieldHeight = i
};
Match3Level.prototype.gridToStageX = function (e) {
    return this.offsetX + (e + .5) * this.tileSize
};
Match3Level.prototype.gridToStageY = function (e) {
    return this.offsetY + (e + .5) * this.tileSize
};
Match3Level.prototype.stageToGridX = function (e) {
    return (e - this.offsetX) / this.tileSize
};
Match3Level.prototype.stageToGridY = function (e) {
    return (e - this.offsetY) / this.tileSize
};
Match3Level.prototype.getCellCenterCoordinatesX = function (e) {
    return this.offsetX + (e + .5) * this.tileSize
};
Match3Level.prototype.getCellCenterCoordinatesY = function (e, t) {
    return this.offsetY + (t + .5) * this.tileSize
};
Object.defineProperty(Match3Level.prototype, "isWaitingForTarget", {
    get: function () {
        return this.showTargetIn > 0
    },
    enumerable: true,
    configurable: true
});
Match3Level.prototype.reset = function () {
    if (this.isActive) {
       Utils.removeClip(this);
        this.isEnded = false;
        this.currentMove = 0;
        this.playLoseIn = 1.3;
        this.isStuck = false;
        // this.uncache();
        this.isPaused = false;
        this._isHardLocked = false;
        this.showTargetIn = .3;
        this.isGenerated = false;
        this.showWinMenuIn = -1;
        var e = 0;
        var t = 0;
        for (var n = 0; n < this.fieldWidth; ++n) {
            for (var r = 0; r < this.fieldHeight; ++r)
                this.cells[n][r].reset();
        }
        var i = this.objects.length;
        for (var s = 0; s < i; ++s)
            this.objects[s].destroy();
        this.objects = [];
        this.target = null;
        this.hud.reset();
        this.needToUpdateBack = true;
    }
};
Match3Level.bonusScores = [0, 80, 80, 100, 150, 110, 180, 180, 250, 280, 500];